package com.es.piomessages;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.es.piomessages.model.AlarmeEntrega;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class SettingsActivity extends PreferenceActivity 
implements OnSharedPreferenceChangeListener {

	public static final String KEY_PREF_NOTIFY = "notificar";
	public static final String KEY_PREF_TIME = "time";
	public static final String DEFAULT_TIME = "18:00";
	public static final boolean DEFAULT_NOTIFY = true;
	private PendingIntent notifyIntent;
	private Intent alarmIntent;
	boolean notify;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);
		alarmIntent = (Intent) getIntent().getExtras().get("alarmIntent");
	}

	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
		.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
		.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if(key.equals(KEY_PREF_NOTIFY) || key.equals(KEY_PREF_TIME)){
			notify = sharedPreferences.getBoolean(KEY_PREF_NOTIFY, DEFAULT_NOTIFY);
			
			//ativar alarme
			String time = sharedPreferences.getString(KEY_PREF_TIME, DEFAULT_TIME);

			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
				Date date = dateFormat.parse(time);

				GregorianCalendar cal = new GregorianCalendar();
				cal.set(Calendar.HOUR_OF_DAY, date.getHours());
				cal.set(Calendar.MINUTE, date.getMinutes());
				cal.set(Calendar.SECOND, 2);
				
				System.out.println("Horas: " + date.getHours());
				System.out.println("Minutos: " + date.getMinutes());
				System.out.println("calSegundos: " + cal.getTime().getSeconds());
				System.out.println("calHoras: " + cal.getTime().getHours());
				System.out.println("calMinutos: " + cal.getTime().getMinutes());
				
				System.out.println("this Intent: " + alarmIntent);
				System.out.println("that intent: " + MainActivity.alarmIntent);				
				System.out.println("Intent igual:" + alarmIntent.filterEquals(MainActivity.alarmIntent));
				
				notifyIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
				AlarmManager mgr = (AlarmManager) getSystemService(ALARM_SERVICE);
				mgr.cancel(notifyIntent);

				mgr = (AlarmManager) getSystemService(ALARM_SERVICE);
				mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, notifyIntent);

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}